﻿using Common;
using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.InfantBaby
{
    public partial class InfantProducAfterPrint : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        string Id;
        Tb_User LoginUser;
        ElectronicSignature es;
        #endregion

        #region 构造函数
        public InfantProducAfterPrint(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            Id = id;
            es = new ElectronicSignature(this.LoginUser);
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\产时产后记录单.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表取数事件
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecord(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.FetationAfter);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.Where(p => p.Id == Convert.ToInt32(Id)).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    InfantProducAfter ps = XmlUtil.XMLToObject<InfantProducAfter>(record.NursingFormData, Encoding.UTF8);
                    string image1 = "";
                    string RYHS = ps.DianYanZhe;
                    if (!string.IsNullOrEmpty(RYHS))
                    {
                        image1 = es.GetElectricSignBase64(record.AreaId, ps.DianYanZhe);
                    }

                    string renshenhebingzheng = GetRshbzStr(ps);
                    string renshenbingfazheng = GetRsbfzStr(ps);
                    DataRow row = dt.NewRow();
                    //护士字段
                    row["PATIENT_NAME"] = PatientInfo.PATIENT_NAME;
                    row["BED_NO"] = PatientInfo.BED_NO;
                    row["PATIENT_ID"] = PatientInfo.PATIENT_ID;
                    row["chanci"] = ps.ChanCi;
                    row["xingbie"] = ps.XingBie;
                    row["RenShenZhouShu"] = ps.RenShenZhouShu;
                    row["ChuShengShiJian"] = ps.ChuShengShiJian;
                    row["TaiWei"] = ps.TaiWei;
                    row["FenMianFangShi"] = ps.FenMianFangShi;
                    row["tizhong"] = ps.TiZhong;
                    row["shenchang"] = ps.ShenChang;
                    row["bingfazheng"] = renshenbingfazheng;
                    row["hbsag"] = ps.HBsAg;
                    row["yangshui"] = ps.YangShui;
                    row["ganran"] = ps.GanRan;
                    row["taiergongneijiongpo"] = ps.TaiErGongNeiJiongPo;
                    row["taiergongneijiongpoText"] = ps.TaiErGongNeiJiongPoText;
                    row["qidaiyichang"] = ps.QiDaiYiChang;
                    row["kainaishijian"] = ps.KaiNaiShiJian;
                    row["meifenzhongxinlv4"] = ps.MeiFenZhongXinLv4;
                    row["meifenzhongxinlv5"] = ps.MeiFenZhongXinLv5;
                    row["meifenzhongxinlv6"] = ps.MeiFenZhongXinLv6;
                    row["huxi4"] = ps.HuXi4;
                    row["huxi5"] = ps.HuXi5;
                    row["huxi6"] = ps.HuXi6;
                    row["jizhangli4"] = ps.JiZhangLi4;
                    row["jizhangli5"] = ps.JiZhangLi5;
                    row["jizhangli6"] = ps.JiZhangLi6;
                    row["houfanshe4"] = ps.HouFanShe4;
                    row["houfanshe5"] = ps.HouFanShe5;
                    row["houfanshe6"] = ps.HouFanShe6;
                    row["pifuyanse4"] = ps.PiFuYanSe4;
                    row["pifuyanse5"] = ps.PiFuYanSe5;
                    row["pifuyanse6"] = ps.PiFuYanSe6;
                    row["zongpingfenshu4"] = ps.ZongPingFenShu4;
                    row["zongpingfenshu5"] = ps.ZongPingFenShu5;
                    row["zongpingfenshu6"] = ps.ZongPingFenShu6;
                    row["jieshengzhe"] = ps.JieShengZhe;
                    row["dianyanzhe"] = ps.DianYanZhe;


                    row["renshenhebingzheng"] = renshenhebingzheng;
                    row["renshenhebingzheng1"] = ps.RenShenHeBingZheng;
                    row["renshenhebingzheng2"] = ps.RenShenHeBingZheng;
                    row["renshenhebingzheng3"] = ps.RenShenHeBingZheng;
                    row["renshenhebingzheng4"] = ps.RenShenHeBingZheng;
                    row["renshenhebingzhengText"] = ps.QiTaNeiWaiKeJiBing;
                    row["zaoposhui"] = ps.ZaoPoShui;
                    row["zhichan"] = ps.ZhiChan;
                    row["zhichanText"] = ps.ZhiChanText;
                    row["taipanyichang"] = ps.TaiPanYiChang;
                    //以下是医生字段
                    row["TiGe_TiWen"] = ps.TiGe_TiWen;
                    row["TiGe_HuXi"] = ps.TiGe_HuXi;
                    row["TiGe_XinLv"] = ps.TiGe_XinLv;
                    row["TiGe_KuSheng"] = ps.TiGe_KuSheng;
                    row["TiGe_LianSe"] = ps.TiGe_LianSe;
                    row["TiGe_YanSe"] = ps.TiGe_YanSe;
                    row["TiGe_ChuXueDian"] = ps.TiGe_ChuXueDian;
                    row["TiGe_YingPi"] = ps.TiGe_YingPi;
                    row["TiGe_ShuiZhong"] = ps.TiGe_ShuiZhong;
                    row["TiGe_TuoPi"] = ps.TiGe_TuoPi;
                    row["TiGe_ChanLiu"] = ps.TiGe_ChanLiu;
                    row["TiGe_XueZhong"] = ps.TiGe_XueZhong;
                    row["TiGe_KouQiang"] = ps.TiGe_KouQiang;
                    row["TiGe_X_WaiXing"] = ps.TiGe_X_WaiXing;
                    row["TiGe_X_Xin"] = ps.TiGe_X_Xin;
                    row["TiGe_X_Fei"] = ps.TiGe_X_Fei;
                    row["TiGe_F_WaiXing"] = ps.TiGe_F_WaiXing;
                    row["TiGe_F_Gan"] = ps.TiGe_F_Gan;
                    row["TiGe_F_Pi"] = ps.TiGe_F_Pi;
                    row["TiGe_F_QiDai"] = ps.TiGe_F_QiDai;
                    row["TiGe_SiZhiZhangLi"] = ps.TiGe_SiZhiZhangLi;
                    row["TiGe_MOrO"] = ps.TiGe_MOrO;
                    row["TiGe_GangMen"] = ps.TiGe_GangMen;
                    row["TiGe_WaiShengZhiQi"] = ps.TiGe_WaiShengZhiQi;
                    row["TiGe_JiXing"] = ps.TiGe_JiXing;
                    row["TiGe_ChuBuYinXiang"] = ps.TiGe_ChuBuYinXiang;
                    row["TiGe_JianChaZhe"] = ps.TiGe_JianChaZhe;
                    row["TiGe_ZhiLiaoJingGuo"] = ps.TiGe_ZhiLiaoJingGuo;
                    row["TiGe_ZhiLiaoJingGuo_YiSheng"] = ps.TiGe_ZhiLiaoJingGuo_YiSheng;
                    row["TiGe_ChuYuan_Xin"] = ps.TiGe_ChuYuan_Xin;
                    row["TiGe_ChuYuan_Fei"] = ps.TiGe_ChuYuan_Fei;
                    row["TiGe_ChuYuan_Gan"] = ps.TiGe_ChuYuan_Gan;
                    row["TiGe_ChuYuan_Pi"] = ps.TiGe_ChuYuan_Pi;
                    row["TiGe_ChuYuan_Qi"] = ps.TiGe_ChuYuan_Qi;
                    row["TiGe_ChuYuan_QiTa"] = ps.TiGe_ChuYuan_QiTa;
                    row["TiGe_ChuYuanZhenDuan"] = ps.TiGe_ChuYuanZhenDuan;
                    row["TiGe_ZhiLiaoZhuanGui"] = ps.TiGe_ZhiLiaoZhuanGui;
                    row["TiGe_ChuYuanYiZhu"] = ps.TiGe_ChuYuanYiZhu;
                    row["TiGe_ChuYuanRiQi"] = ps.TiGe_ChuYuanRiQi;
                    dt.Rows.Add(row);
                }

                //dt.DefaultView.Sort = "riqi ASC,shijian ASC";
                DataTable data = dt.DefaultView.ToTable();
                Utility.FillRecordToReport(Report, data);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }


        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("chanci", typeof(string));
            dt.Columns.Add("xingbie", typeof(string));
            dt.Columns.Add("RenShenZhouShu", typeof(string));
            dt.Columns.Add("ChuShengShiJian", typeof(string));
            dt.Columns.Add("TaiWei", typeof(string));
            dt.Columns.Add("FenMianFangShi", typeof(string));
            dt.Columns.Add("tizhong", typeof(string));
            dt.Columns.Add("shenchang", typeof(string));
            dt.Columns.Add("bingfazheng", typeof(string));
            dt.Columns.Add("hbsag", typeof(string));
            dt.Columns.Add("yangshui", typeof(string));
            dt.Columns.Add("ganran", typeof(string));
            dt.Columns.Add("taiergongneijiongpo", typeof(string));
            dt.Columns.Add("taiergongneijiongpoText", typeof(string));
            dt.Columns.Add("qidaiyichang", typeof(string));
            dt.Columns.Add("kainaishijian", typeof(string));
            dt.Columns.Add("meifenzhongxinlv4", typeof(string));
            dt.Columns.Add("meifenzhongxinlv5", typeof(string));
            dt.Columns.Add("meifenzhongxinlv6", typeof(string));
            dt.Columns.Add("huxi4", typeof(string));
            dt.Columns.Add("huxi5", typeof(string));
            dt.Columns.Add("huxi6", typeof(string));
            dt.Columns.Add("jizhangli4", typeof(string));
            dt.Columns.Add("jizhangli5", typeof(string));
            dt.Columns.Add("jizhangli6", typeof(string));
            dt.Columns.Add("houfanshe4", typeof(string));
            dt.Columns.Add("houfanshe5", typeof(string));
            dt.Columns.Add("houfanshe6", typeof(string));
            dt.Columns.Add("pifuyanse4", typeof(string));
            dt.Columns.Add("pifuyanse5", typeof(string));
            dt.Columns.Add("pifuyanse6", typeof(string));
            dt.Columns.Add("zongpingfenshu4", typeof(string));
            dt.Columns.Add("zongpingfenshu5", typeof(string));
            dt.Columns.Add("zongpingfenshu6", typeof(string));
            dt.Columns.Add("jieshengzhe", typeof(string));
            dt.Columns.Add("dianyanzhe", typeof(string));
            dt.Columns.Add("renshenhebingzheng", typeof(string));
            dt.Columns.Add("renshenhebingzheng1", typeof(string));
            dt.Columns.Add("renshenhebingzheng2", typeof(string));
            dt.Columns.Add("renshenhebingzheng3", typeof(string));
            dt.Columns.Add("renshenhebingzheng4", typeof(string));
            dt.Columns.Add("renshenhebingzhengText", typeof(string));
            dt.Columns.Add("zaoposhui", typeof(string));
            dt.Columns.Add("zhichan", typeof(string));
            dt.Columns.Add("zhichanText", typeof(string));
            dt.Columns.Add("taipanyichang", typeof(string));
            //以下医生字段
            dt.Columns.Add("TiGe_TiWen", typeof(string));
            dt.Columns.Add("TiGe_HuXi", typeof(string));
            dt.Columns.Add("TiGe_XinLv", typeof(string));
            dt.Columns.Add("TiGe_KuSheng", typeof(string));
            dt.Columns.Add("TiGe_LianSe", typeof(string));
            dt.Columns.Add("TiGe_YanSe", typeof(string));
            dt.Columns.Add("TiGe_ChuXueDian", typeof(string));
            dt.Columns.Add("TiGe_YingPi", typeof(string));
            dt.Columns.Add("TiGe_ShuiZhong", typeof(string));
            dt.Columns.Add("TiGe_TuoPi", typeof(string));
            dt.Columns.Add("TiGe_ChanLiu", typeof(string));
            dt.Columns.Add("TiGe_XueZhong", typeof(string));
            dt.Columns.Add("TiGe_KouQiang", typeof(string));
            dt.Columns.Add("TiGe_X_WaiXing", typeof(string));
            dt.Columns.Add("TiGe_X_Xin", typeof(string));
            dt.Columns.Add("TiGe_X_Fei", typeof(string));
            dt.Columns.Add("TiGe_F_WaiXing", typeof(string));
            dt.Columns.Add("TiGe_F_Gan", typeof(string));
            dt.Columns.Add("TiGe_F_Pi", typeof(string));
            dt.Columns.Add("TiGe_F_QiDai", typeof(string));
            dt.Columns.Add("TiGe_SiZhiZhangLi", typeof(string));
            dt.Columns.Add("TiGe_MOrO", typeof(string));
            dt.Columns.Add("TiGe_GangMen", typeof(string));
            dt.Columns.Add("TiGe_WaiShengZhiQi", typeof(string));
            dt.Columns.Add("TiGe_JiXing", typeof(string));
            dt.Columns.Add("TiGe_ChuBuYinXiang", typeof(string));
            dt.Columns.Add("TiGe_JianChaZhe", typeof(string));
            dt.Columns.Add("TiGe_ZhiLiaoJingGuo", typeof(string));
            dt.Columns.Add("TiGe_ZhiLiaoJingGuo_YiSheng", typeof(string));
            dt.Columns.Add("TiGe_ChuYuan_Xin", typeof(string));
            dt.Columns.Add("TiGe_ChuYuan_Fei", typeof(string));
            dt.Columns.Add("TiGe_ChuYuan_Gan", typeof(string));
            dt.Columns.Add("TiGe_ChuYuan_Pi", typeof(string));
            dt.Columns.Add("TiGe_ChuYuan_Qi", typeof(string));
            dt.Columns.Add("TiGe_ChuYuan_QiTa", typeof(string));
            dt.Columns.Add("TiGe_ChuYuanZhenDuan", typeof(string));
            dt.Columns.Add("TiGe_ZhiLiaoZhuanGui", typeof(string));
            dt.Columns.Add("TiGe_ChuYuanYiZhu", typeof(string));
            dt.Columns.Add("TiGe_ChuYuanRiQi", typeof(string));
        }


        private string GetRsbfzStr(InfantProducAfter ps)
        {
            string bfz = string.Empty;
            if (string.IsNullOrEmpty(ps.BingFaZhengChks)) return bfz;
            if (ps.BingFaZhengChks.Split('#')[0].Equals("1"))
                bfz += "流产、";
            if (ps.BingFaZhengChks.Split('#')[1].Equals("1"))
                bfz += "早产、";
            if (ps.BingFaZhengChks.Split('#')[2].Equals("1"))
                bfz += "过期妊娠、";
            if (ps.BingFaZhengChks.Split('#')[3].Equals("1"))
                bfz += "异位妊娠、";
            if (ps.BingFaZhengChks.Split('#')[4].Equals("1"))
                bfz += "妊娠剧吐、";
            if (ps.BingFaZhengChks.Split('#')[5].Equals("1"))
                bfz += "妊娠期高血压疾病、";
            if (ps.BingFaZhengChks.Split('#')[6].Equals("1"))
                bfz += "前置胎盘、";
            if (ps.BingFaZhengChks.Split('#')[7].Equals("1"))
                bfz += "胎盘早剥、";
            if (ps.BingFaZhengChks.Split('#')[8].Equals("1"))
                bfz += "多胎妊娠、";
            if (ps.BingFaZhengChks.Split('#')[9].Equals("1"))
                bfz += "羊水过多、";
            if (ps.BingFaZhengChks.Split('#')[10].Equals("1"))
                bfz += "羊水过少、";
            if (ps.BingFaZhengChks.Split('#')[11].Equals("1"))
                bfz += "母儿血型不合、";
            if (ps.BingFaZhengChks.Split('#')[12].Equals("1"))
                bfz += "胎儿生长受限、";
            if (ps.BingFaZhengChks.Split('#')[13].Equals("1"))
                bfz += "死胎、";


            if (ps.BingFaZhengChks.Split('#').Length >= 17 && ps.BingFaZhengChks.Split('#')[16].Equals("1"))
                bfz += "脐带绕颈、";
            if (ps.BingFaZhengChks.Split('#').Length >= 18 && ps.BingFaZhengChks.Split('#')[17].Equals("1"))
                bfz += "妊娠期糖尿病、";
            if (ps.BingFaZhengChks.Split('#').Length >= 19 && ps.BingFaZhengChks.Split('#')[18].Equals("1"))
                bfz += "胎儿窘迫、";
            if (ps.BingFaZhengChks.Split('#').Length >= 20 && ps.BingFaZhengChks.Split('#')[19].Equals("1"))
                bfz += "脐带扭转、";
            if (ps.BingFaZhengChks.Split('#').Length >= 21 && ps.BingFaZhengChks.Split('#')[20].Equals("1"))
                bfz += "球拍状胎盘、";
            if (ps.BingFaZhengChks.Split('#').Length >= 22 && ps.BingFaZhengChks.Split('#')[21].Equals("1"))
                bfz += "新生儿轻度窒息、";
            if (ps.BingFaZhengChks.Split('#').Length >= 23 && ps.BingFaZhengChks.Split('#')[22].Equals("1"))
                bfz += "新生儿中度窒息、";
            if (ps.BingFaZhengChks.Split('#').Length >= 24 && ps.BingFaZhengChks.Split('#')[23].Equals("1"))
                bfz += "新生儿重度窒息、";


            if (ps.BingFaZhengChks.Split('#')[14].Equals("1"))
                bfz += "其他严重内外科疾病、";
            if (ps.BingFaZhengChks.Split('#').Length >= 16 && ps.BingFaZhengChks.Split('#')[15].Equals("1"))
                bfz += ps.BingFaZheng + "、";
            if (bfz.Length > 0)
                bfz = bfz.Substring(0, bfz.Length - 1);
            return bfz;
        }

        private static string GetRshbzStr(InfantProducAfter ps)
        {
            string hbz = string.Empty;
            if (string.IsNullOrEmpty(ps.RenShenHeBingZheng)) return hbz;
            if (ps.RenShenHeBingZheng.Split('#')[0].Equals("1"))
                hbz += "无、";
            if (ps.RenShenHeBingZheng.Split('#')[1].Equals("1"))
                hbz += "贫血、";
            if (ps.RenShenHeBingZheng.Split('#')[2].Equals("1"))
                hbz += "心脏病、";
            if (ps.RenShenHeBingZheng.Split('#')[3].Equals("1"))
                hbz += "肾脏疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[4].Equals("1"))
                hbz += "血液系统疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[5].Equals("1"))
                hbz += "肝炎或肝损害、";
            if (ps.RenShenHeBingZheng.Split('#')[6].Equals("1"))
                hbz += "活动性肺结核、";
            if (ps.RenShenHeBingZheng.Split('#')[7].Equals("1"))
                hbz += "糖尿病、";
            if (ps.RenShenHeBingZheng.Split('#')[8].Equals("1"))
                hbz += "甲状腺功能亢进症、";
            if (ps.RenShenHeBingZheng.Split('#')[9].Equals("1"))
                hbz += "精神病或神经系统疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[10].Equals("1"))
                hbz += "妊娠合并免疫系统疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[11].Equals("1"))
                hbz += "卵巢或子宫肿瘤、";
            if (ps.RenShenHeBingZheng.Split('#')[12].Equals("1"))
                hbz += "孕期感染性疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[13].Equals("1"))
                hbz += "传播性疾病(含艾滋病)、";
            if (ps.RenShenHeBingZheng.Split('#')[14].Equals("1"))
                hbz += "妊娠合并心律失常、";
            if (ps.RenShenHeBingZheng.Split('#')[15].Equals("1"))
                hbz += "妊娠合并血小板减少性紫癜、";
            if (ps.RenShenHeBingZheng.Split('#')[16].Equals("1"))
                hbz += "感染性疾病合并妊娠、";
            if (ps.RenShenHeBingZheng.Split('#')[17].Equals("1"))
                hbz += "妊娠合并生殖器畸形、";
            if (ps.RenShenHeBingZheng.Split('#')[18].Equals("1"))
                hbz += "其他严重内外科疾病、";
            if (ps.RenShenHeBingZheng.Split('#').Length >= 20 && ps.RenShenHeBingZheng.Split('#')[19].Equals("1"))
                hbz += ps.QiTaNeiWaiKeJiBing + "、";
            if (hbz.Length > 0)
                hbz = hbz.Substring(0, hbz.Length - 1);
            return hbz;
        }
        #endregion

        #region 窗体加载启动锐浪报表
        private void InfantProducAfterPrint_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
    }
}
